home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 46 / Amiga Format CD46 (1999-10-20)(Future Publishing)(GB)[!][issue 1999-12].iso / -serious- / programming / other / hexy / src / hexy_winhunk.c < prev    next >
C/C++ Source or Header  |  1999-09-06  |  11KB  |  430 lines

  1.  
  2. /*
  3.  * [!BGN - MACHINE GENERATED - DO NOT EDIT THIS HEADER]
  4.  *
  5.  * Program   : Hexy (Binary file viewer/editor for the Amiga.)
  6.  * Version   : 1.6
  7.  * File      : Work:Source/!WIP/HisoftProjects/Hexy/Hexy_winhunk.c
  8.  * Author    : Andrew Bell
  9.  * Copyright : Copyright © 1998-1999 Andrew Bell (See GNU GPL)
  10.  * Created   : Saturday 28-Feb-98 16:00:00
  11.  * Modified  : Sunday 22-Aug-99 23:31:45
  12.  * Comment   : 
  13.  *
  14.  * (Generated with StampSource 1.2 by Andrew Bell)
  15.  *
  16.  * [!END - MACHINE GENERATED - DO NOT EDIT THIS HEADER]
  17.  *
  18.  */
  19.  
  20.  
  21. /* Created: Sun/09/Aug/1998 */
  22.  
  23. #include <Hexy.h>
  24.  
  25. /*
  26.  *  Hexy, binary file viewer and editor for the Amiga.
  27.  *  Copyright (C) 1999 Andrew Bell
  28.  *
  29.  *  Author's email address: andrew.ab2000@bigfoot.com
  30.  *
  31.  *  This program is free software; you can redistribute it and/or modify
  32.  *  it under the terms of the GNU General Public License as published by
  33.  *  the Free Software Foundation; either version 2 of the License, or
  34.  *  (at your option) any later version.
  35.  *
  36.  *  This program is distributed in the hope that it will be useful,
  37.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  38.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  39.  *  GNU General Public License for more details.
  40.  *
  41.  *  You should have received a copy of the GNU General Public License
  42.  *  along with this program; if not, write to the Free Software
  43.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  44.  *
  45.  */
  46.  
  47. Prototype void ViewHunkListWindow( void );
  48. Prototype void ClearHunkListWindow( void );
  49. Prototype void UpdateHunkListWindow( void );
  50. Prototype void IDCMP_HUNKLIST( void );
  51. Prototype void BuildHunkList( void );
  52. Prototype void RemoveHunkList( void );
  53. Prototype struct HunkListNode *AddHunkListNode( UBYTE *String, APTR Fmt, ULONG Offset );
  54. Prototype struct HunkListNode *GetHLNAddress( ULONG Index );
  55.  
  56. struct List HunkList;
  57.  
  58. struct HunkListNode
  59. {
  60.   struct Node HLN_Node;
  61.   ULONG       HLN_Offset;
  62.   ULONG       HLN_Length;           /* currently not used */
  63.   UBYTE       HLN_ViewString[128];
  64. };
  65.  
  66. void ViewHunkListWindow( void )
  67. {
  68.   /*************************************************
  69.    *
  70.    * Show the HunkList window 
  71.    *
  72.    */
  73.  
  74.   if ( HUNKLISTWnd )
  75.   {
  76.     WindowToFront( HUNKLISTWnd );
  77.     ActivateWindow( HUNKLISTWnd );
  78.     return;
  79.   }
  80.  
  81.   if (!ValidFile(&VC)) return;
  82.  
  83.   if (!OpenHUNKLISTWindow())
  84.   {
  85.     SwapPort(HUNKLISTWnd, WinPort);
  86.  
  87.     BuildHunkList();
  88.  
  89.     UpdateHunkListWindow();
  90.   }
  91.   else
  92.   {
  93.     CloseHUNKLISTWindow();
  94.   }
  95. }
  96.  
  97. void ClearHunkListWindow( void )
  98. {
  99.   /*************************************************
  100.    *
  101.    * Hide the HunkList window 
  102.    *
  103.    */
  104.  
  105.   if (!HUNKLISTWnd) return;
  106.  
  107.   FlushWindow(HUNKLISTWnd);
  108.   CloseHUNKLISTWindow();
  109.   RemoveHunkList();
  110. }
  111.  
  112. void UpdateHunkListWindow( void )
  113. {
  114.   /*************************************************
  115.    *
  116.    * 
  117.    *
  118.    */
  119. }
  120.  
  121. void IDCMP_HUNKLIST( void )
  122. {
  123.   /*************************************************
  124.    *
  125.    * Process any IDCMP events relating to the HunkList window
  126.    *
  127.    */
  128.  
  129.   if (IDCMP_CheckRAWKEYS()) return;
  130.   
  131.   switch(IM.Class)
  132.   {
  133.     case IDCMP_CLOSEWINDOW:
  134.       ClearHunkListWindow();
  135.       break;
  136.  
  137.     case IDCMP_MOUSEBUTTONS:
  138.     case IDCMP_ACTIVEWINDOW:
  139.       break;
  140.  
  141.     case IDCMP_REFRESHWINDOW:
  142.       /* HUNKLISTRender(); */
  143.       break;
  144.  
  145.     case IDCMP_GADGETUP:
  146.       switch(Gad->GadgetID)
  147.       {
  148.         case GD_HLLV:
  149.           break;
  150.         case GD_HLDONE:
  151.           ClearHunkListWindow();
  152.           break;
  153.         case GD_HLGOTO:
  154.         {
  155.           ULONG Sel = -1;
  156.  
  157.           GT_GetGadgetAttrs(HUNKLISTGadgets[GD_HLLV], HUNKLISTWnd, NULL,
  158.             GTLV_Selected, &Sel, TAG_DONE );
  159.  
  160.           if (Sel != -1)
  161.           {
  162.             struct HunkListNode *HLN = (struct HunkListNode *) GetHLNAddress( Sel );
  163.             if ( HLN )
  164.             {
  165.               if (HLN->HLN_Offset != -1)
  166.               {
  167.                 if (HLN->HLN_Offset <= VC.VC_FileLength)
  168.                 {
  169.                   VC.VC_CurrentPoint = HLN->HLN_Offset;
  170.                   UpdateView(&VC, NULL);
  171.                   SetVDragBar(&VC);
  172.                 }
  173.                 else DisplayBeep(HexyScreen);
  174.               }
  175.               else DisplayBeep(HexyScreen);
  176.             }
  177.             else DisplayBeep(HexyScreen);
  178.           }
  179.           else DisplayBeep(HexyScreen);
  180.           break;
  181.         }
  182.         default:
  183.           break;
  184.       }
  185.       break;
  186.  
  187.     default:
  188.       break;
  189.   } /* switch(CLASS) */
  190. }
  191.  
  192. void BuildHunkList( void ) /* Put hunk list in view control */
  193. {
  194.   /*************************************************
  195.    *
  196.    * The the main hunk list for the HunkList window
  197.    *
  198.    */
  199.  
  200.   /* Build a linked list for the hunk window LV. */
  201.  
  202.   BOOL running = TRUE;
  203.   ULONG *Ptr = (ULONG *) VC.VC_FileAddress;
  204.   ULONG *UpperPtr = Ptr;
  205.   ((UBYTE *)UpperPtr) += VC.VC_FileLength;
  206.  
  207.   NewList( (struct List *) &HunkList );
  208.  
  209.   if (*Ptr != HUNK_HEADER)
  210.   {
  211.     AddHunkListNode( "This file is not executable!", NULL , ~NULL);
  212.   }
  213.   else
  214.   {
  215.     while( Ptr < UpperPtr && running == TRUE )
  216.     {
  217.       ULONG Offset = ((UBYTE *)Ptr) - VC.VC_FileAddress;
  218.       ULONG HunkID = *Ptr++ & 0x0000ffff;
  219.       ULONG Length = *Ptr++;                   /* Length in longwords */
  220.  
  221.       switch( HunkID )
  222.       {
  223.         case HUNK_HEADER:
  224.         {
  225.           ULONG AmountOfHunks;
  226.           stream[0] = Offset;
  227.           stream[1] = *Ptr++; /* Total */
  228.           AmountOfHunks = Ptr[1] - Ptr[0];
  229.           stream[2] = *Ptr++; /* Rng1 */
  230.           stream[3] = *Ptr++; /* Rng2 */
  231.           Ptr += AmountOfHunks + 1;
  232.           AddHunkListNode( "0x%08lx HUNK_HEADER   %lu hunks, %lu to %lu", &stream, Offset );
  233.           break;
  234.         }
  235.  
  236.         case HUNK_CODE:
  237.         {
  238.           stream[0] = Offset;
  239.           stream[1] = Length << 2;
  240.           AddHunkListNode( "0x%08lx HUNK_CODE     %lu bytes in size", &stream, Offset );
  241.           Ptr += Length;
  242.           break;
  243.         }
  244.  
  245.         case HUNK_DATA:
  246.           stream[0] = Offset;
  247.           stream[1] = Length << 2;
  248.           AddHunkListNode( "0x%08lx HUNK_DATA     %lu bytes in size", &stream, Offset );
  249.           Ptr += Length;
  250.           break;
  251.  
  252.         case HUNK_BSS:
  253.           stream[0] = Offset;
  254.           stream[1] = Length << 2;
  255.           AddHunkListNode( "0x%08lx HUNK_BSS      %lu bytes in size", &stream, Offset );
  256.           break;
  257.  
  258.         case HUNK_END:
  259.           stream[0] = Offset;
  260.           Ptr--;
  261.           AddHunkListNode( "0x%08lx HUNK_END", &stream, Offset );
  262.           break;
  263.  
  264.         case HUNK_DEBUG:
  265.           stream[0] = Offset;
  266.           stream[1] = Length << 2;
  267.           AddHunkListNode( "0x%08lx HUNK_DEBUG    %lu bytes in size", &stream, Offset );
  268.           Ptr += Length;
  269.           break;
  270.  
  271.         case HUNK_RELOC32:
  272.         {
  273.           ULONG Amount;
  274.           stream[1] = stream[2] = 0;
  275.           
  276.           Ptr--; /* Fix */
  277.           while (Amount = *Ptr++)
  278.           {
  279.             Ptr++; /* Skip hunk index */
  280.             Ptr += Amount;
  281.             stream[1] += Amount;
  282.             stream[2]++;
  283.           }
  284.           stream[0] = Offset;
  285.           AddHunkListNode( "0x%08lx HUNK_RELOC32  %lu entries, %lu hunks", &stream, Offset );
  286.           break;
  287.         }
  288.  
  289.         case HUNK_DREL32:
  290.         {
  291.           UWORD Amount;
  292.           stream[1] = stream[2] = NULL;
  293.           
  294.           Ptr--; /* Fix */
  295.           while (Amount = *((UWORD *)Ptr)++)
  296.           {
  297.             ((UWORD *)Ptr)++ ; /* Skip hunk index */
  298.             ((UWORD *)Ptr) += Amount;
  299.             stream[1] += Amount;
  300.             stream[2]++;
  301.           }
  302.           /* Longword align pointer */
  303.           while( (( ((ULONG)Ptr) >> 2) << 2) != ((ULONG)Ptr))
  304.           {   
  305.             ((UBYTE *)Ptr)++;
  306.           } 
  307.           stream[0] = Offset;
  308.           AddHunkListNode( "0x%08lx HUNK_DREL32   %lu entries, %lu hunks", &stream, Offset );
  309.           break;
  310.         }
  311.  
  312.         case HUNK_SYMBOL: /* [Length] [Symbol] [Offset] */
  313.         {
  314.           ULONG SymbolLen;
  315.           Ptr--;
  316.           stream[1] = NULL;
  317.           while (SymbolLen = *Ptr++)
  318.           {
  319.             Ptr += SymbolLen + 1; /* Skip symbol plus offset */
  320.             stream[1]++;
  321.           }
  322.           stream[0] = Offset;
  323.           AddHunkListNode( "0x%08lx HUNK_SYMBOL   %lu symbol entries", &stream, Offset );
  324.           break;
  325.         }
  326.  
  327.         case HUNK_RELOC32SHORT:
  328.           stream[0] = Offset;
  329.           AddHunkListNode( "0x%08lx HUNK_RELOC32SHORT Unsupported", &stream, Offset );
  330.           running = FALSE;
  331.           break;
  332.  
  333.         case HUNK_BREAK:
  334.           stream[0] = Offset;
  335.           AddHunkListNode( "0x%08lx HUNK_BREAK    Unsupported", &stream, Offset );
  336.           running = FALSE;
  337.           break;
  338.  
  339.         case HUNK_OVERLAY:
  340.           stream[0] = Offset;
  341.           AddHunkListNode( "0x%08lx HUNK_OVERLAY  Unsupported", &stream, Offset );
  342.           running = FALSE;
  343.           break;
  344.  
  345.         default:
  346.           stream[0] = Offset;
  347.           AddHunkListNode( "0x%08lx HUNK_<unknown>", &stream, Offset );
  348.           running = FALSE;
  349.           break;
  350.       }
  351.     }
  352.   }
  353.  
  354.   GT_SetGadgetAttrs(HUNKLISTGadgets[GD_HLLV], HUNKLISTWnd, NULL,
  355.     GTLV_Labels, &HunkList,
  356.     TAG_DONE );
  357. }
  358.  
  359. void RemoveHunkList( void )
  360. {
  361.   /*************************************************
  362.    *
  363.    * Free the hunk list
  364.    *
  365.    */
  366.  
  367.   /* Listview must be deleted before calling this function */
  368.  
  369.   struct HunkListNode *TempHLN = NULL;
  370.   struct HunkListNode *HLN = NULL;
  371.  
  372.   for ( HLN = (struct HunkListNode *) HunkList.lh_Head; HLN->HLN_Node.ln_Succ;  )
  373.   {
  374.     TempHLN = (struct HunkListNode *) HLN->HLN_Node.ln_Succ;
  375.     FreeVec( HLN );
  376.     HLN = TempHLN;
  377.   }
  378.   NewList( (struct List *) &HunkList );
  379. }
  380.  
  381. struct HunkListNode *AddHunkListNode( UBYTE *String, APTR Fmt, ULONG Offset )
  382. {
  383.   /*************************************************
  384.    *
  385.    * Add an entry to the hunk list 
  386.    *
  387.    */
  388.  
  389.   struct HunkListNode *HLN = (struct HunkListNode *) AllocVec( sizeof( struct HunkListNode ), MEMF_CLEAR );
  390.  
  391.   if ( HLN )
  392.   {
  393.     HLN->HLN_Offset = Offset;
  394.     HLN->HLN_Node.ln_Name = (UBYTE *) &HLN->HLN_ViewString;
  395.     /*Sprintf(String , (UBYTE *) &HLN->HLN_ViewString, Fmt);*/
  396.  
  397.     RawDoFmt(String, Fmt, (void *) &putChProc, (UBYTE *) &HLN->HLN_ViewString);
  398.  
  399.  
  400.     AddTail( (struct List *) &HunkList, (struct Node *) HLN );
  401.   }
  402.  
  403.   return HLN;
  404. }
  405.  
  406. struct HunkListNode *GetHLNAddress( ULONG Index )
  407. {
  408.   /*************************************************
  409.    *
  410.    * Get the address of a HLN 
  411.    *
  412.    */
  413.  
  414.   struct HunkListNode *HLN;
  415.  
  416.   for ( HLN = (struct HunkListNode *) HunkList.lh_Head; HLN->HLN_Node.ln_Succ; HLN = (struct  HunkListNode *) HLN->HLN_Node.ln_Succ )
  417.   {
  418.     if (!Index--) return HLN;
  419.   }
  420.  
  421.   return 0;
  422. }
  423.  
  424. /*************************************************
  425.  *
  426.  * 
  427.  *
  428.  */
  429.  
  430.